package top.zhangjianyong.tools.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import top.zhangjianyong.tools.utils.ApiResponse;

import java.time.LocalTime;

/**
 * @author ： zhangjianyong
 * @date ： 2022/11/28 3:04 PM
 */
@Slf4j
@Aspect
@Component
@Order(999)
public class TimerAspect {

    @Pointcut("@annotation(top.zhangjianyong.tools.annotation.CheckTimer)")
    public void checkPointCut() {

    }

    @Around("checkPointCut()")
    public Object checkTimer(ProceedingJoinPoint pjp) {
        //检查是否为交易时间
        //上午
        log.info("进入时间检测");
        LocalTime ss = LocalTime.of(9, 30);
        LocalTime se = LocalTime.of(11, 30);
        LocalTime xs = LocalTime.of(13, 0);
        LocalTime xe = LocalTime.of(15, 30);
        LocalTime now = LocalTime.now();
        if (now.isBefore(ss)
                || (now.isAfter(se) && now.isBefore(xs))
                || now.isAfter(xe)) {
            log.info("非交易时间。。。");
            return null;
        }
        try {
            log.info("当前时间符合执行");
            return pjp.proceed();
        } catch (Throwable e) {
            log.error(e.getMessage(), e);
            return ApiResponse.fail(e.getMessage());
        }
    }


}
